/**
 * 作者: wgx
 * 日期: 2024-11-13 16:42:19
 * 备注: 派对的座位列表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.CsGameFeast.PBServerFeastSeat;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class ServerFeastSeatModel extends BaseModel implements Model {
    public static final String LISTKEY = "feast_id.user_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // ID
    protected long feastId; // 派对id
    protected long userId; // 参与者的id
    protected int seatPosition; // 座位的位置
    protected String userName = ""; // 用户名字
    protected String icon = ""; // 用户头像
    protected int level; // 等级
    protected int vip; // vip等级
    protected int joinId; // 参加的类型 (feast_jion表的id)
    protected long addTime; // 加入时间

    public void reset() {
    }

    protected String getKey() {
        return "ServerFeastSeat." + feastId + "." + userId;
    }

    protected String getListKey() {
        return "ServerFeastSeat." + feastId;
    }

    protected String getListItemKey() {
        return userId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("server_feast_seat"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // ID
        obj.put("feastId", feastId); // 派对id
        obj.put("userId", userId); // 参与者的id
        obj.put("seatPosition", seatPosition); // 座位的位置
        obj.put("userName", userName); // 用户名字
        obj.put("icon", icon); // 用户头像
        obj.put("level", level); // 等级
        obj.put("vip", vip); // vip等级
        obj.put("joinId", joinId); // 参加的类型 (feast_jion表的id)
        obj.put("addTime", addTime); // 加入时间
        return obj;
    }

    public PBServerFeastSeat.Builder takeInitBuilder() {
        PBServerFeastSeat.Builder sb = PBServerFeastSeat.newBuilder();
        if (userId != 0) {
            sb.setUserId(userId);
        }
        if (seatPosition != 0) {
            sb.setSeatPosition(seatPosition);
        }
        if (!userName.isEmpty()) {
            sb.setUserName(userName);
        }
        if (!icon.isEmpty()) {
            sb.setIcon(icon);
        }
        if (level != 0) {
            sb.setLevel(level);
        }
        if (vip != 0) {
            sb.setVip(vip);
        }
        if (joinId != 0) {
            sb.setJoinId(joinId);
        }
        if (addTime != 0) {
            sb.setAddTime(addTime);
        }
        return sb;
    }

    public PBServerFeastSeat.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBServerFeastSeat.Builder sb = PBServerFeastSeat.newBuilder();
        if (isFieldUpdate("userId")) {
            sb.setUserId(userId);
        }
        if (isFieldUpdate("seatPosition")) {
            sb.setSeatPosition(seatPosition);
        }
        if (isFieldUpdate("userName")) {
            sb.setUserName(userName);
        }
        if (isFieldUpdate("icon")) {
            sb.setIcon(icon);
        }
        if (isFieldUpdate("level")) {
            sb.setLevel(level);
        }
        if (isFieldUpdate("vip")) {
            sb.setVip(vip);
        }
        if (isFieldUpdate("joinId")) {
            sb.setJoinId(joinId);
        }
        if (isFieldUpdate("addTime")) {
            sb.setAddTime(addTime);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getFeastId() {
        return feastId;
    }

    public void putFeastId(long feastId) {
        if (this.feastId != feastId) {
            this.feastId = feastId;
            updateRedisHashField(getKey(), "feastId", this.feastId);
        }
    }

    public void addFeastId(long value) {
        this.feastId += value;
        updateRedisHashField(getKey(), "feastId", this.feastId);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getSeatPosition() {
        return seatPosition;
    }

    public void putSeatPosition(int seatPosition) {
        if (this.seatPosition != seatPosition) {
            this.seatPosition = seatPosition;
            updateRedisHashField(getKey(), "seatPosition", this.seatPosition);
        }
    }

    public void addSeatPosition(int value) {
        this.seatPosition += value;
        updateRedisHashField(getKey(), "seatPosition", this.seatPosition);
    }

    public String getUserName() {
        return userName;
    }

    public void putUserName(String userName) {
        if (!this.userName.equals(userName)) {
            this.userName = userName;
            updateRedisHashField(getKey(), "userName", this.userName);
        }
    }

    public String getIcon() {
        return icon;
    }

    public void putIcon(String icon) {
        if (!this.icon.equals(icon)) {
            this.icon = icon;
            updateRedisHashField(getKey(), "icon", this.icon);
        }
    }

    public int getLevel() {
        return level;
    }

    public void putLevel(int level) {
        if (this.level != level) {
            this.level = level;
            updateRedisHashField(getKey(), "level", this.level);
        }
    }

    public void addLevel(int value) {
        this.level += value;
        updateRedisHashField(getKey(), "level", this.level);
    }

    public int getVip() {
        return vip;
    }

    public void putVip(int vip) {
        if (this.vip != vip) {
            this.vip = vip;
            updateRedisHashField(getKey(), "vip", this.vip);
        }
    }

    public void addVip(int value) {
        this.vip += value;
        updateRedisHashField(getKey(), "vip", this.vip);
    }

    public int getJoinId() {
        return joinId;
    }

    public void putJoinId(int joinId) {
        if (this.joinId != joinId) {
            this.joinId = joinId;
            updateRedisHashField(getKey(), "joinId", this.joinId);
        }
    }

    public void addJoinId(int value) {
        this.joinId += value;
        updateRedisHashField(getKey(), "joinId", this.joinId);
    }

    public long getAddTime() {
        return addTime;
    }

    public void putAddTime(long addTime) {
        if (this.addTime != addTime) {
            this.addTime = addTime;
            updateRedisHashField(getKey(), "addTime", this.addTime);
        }
    }

    public void addAddTime(long value) {
        this.addTime += value;
        updateRedisHashField(getKey(), "addTime", this.addTime);
    }

}